package com.rem.stream.part2;

import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @Author Rem
 * @Date 2020-02-14
 */

public class TestTransaction {

    List<Transaction> transactionList;

    @Before
    public void before() {

        Trader trader1 = new Trader("Raoul", "ShangHai");
        Trader trader2 = new Trader("Bob", "BeiJing");
        Trader trader3 = new Trader("Sady", "XiAn");
        Trader trader4 = new Trader("Jery", "XiAn");
        Trader trader5 = new Trader("Rem", "ShangHai");

        transactionList = Arrays.asList(
                new Transaction(trader1, 2018, 600),
                new Transaction(trader2, 2018, 400),
                new Transaction(trader3, 2019, 600),
                new Transaction(trader4, 2019, 900),
                new Transaction(trader5, 2018, 700),
                new Transaction(trader1, 2019, 500),
                new Transaction(trader2, 2019, 600)
        );
    }


    //1.找出2018年发生的所有交易，并按交易额排序（低到高）
    @Test
    public void test() {
        transactionList.stream()
                .filter(t -> t.getYear().equals(2018))
                // .sorted((t1,t2)-> -t1.getValue().compareTo(t2.getValue()))  //高到低
                .sorted(Comparator.comparingInt(Transaction::getValue))
                .forEach(System.out::println);
    }

    //2.交易员都在那些不同的城市工作过
    @Test
    public void test2() {
        transactionList.stream()
                .map(t -> t.getTrader().getCity())
                .distinct()
                .forEach(System.out::println);
    }

    //3.返回所有来自来自上海的交易员，并按照姓名排序
    @Test
    public void test3() {
        transactionList.stream()
                .filter(e -> e.getTrader().getCity().equals("ShangHai"))
                .map(Transaction::getTrader)
                .distinct()
                .sorted(Comparator.comparing(Trader::getName))
                .forEach(System.out::println);
    }

    //4.返回所有交易员的姓名字符串，按字母顺序排序
    @Test
    public void test4() {
        transactionList.stream()
                .map(t -> t.getTrader().getName())
                .distinct()
                .sorted()
                .forEach(System.out::println);
    }

    //5.有没有交易员是在西安工作的
    @Test
    public void test5() {
        boolean bo = transactionList.stream()
                .anyMatch(t -> t.getTrader().getCity().equals("XiAn"));
        System.out.println(bo);
    }

    //6.打印生活在上海的交易员的所有交易额
    @Test
    public void test6() {
        Optional<Integer> optional = transactionList.stream()
                .filter(e -> e.getTrader().getCity().equals("ShangHai"))
                .map(Transaction::getValue)
                .reduce(Integer::sum);


        int sum = transactionList.stream()
                .filter(e -> e.getTrader().getCity().equals("ShangHai"))
                .mapToInt(Transaction::getValue)
                .sum();

        System.out.println(optional.get());
        System.out.println(sum);
    }

    //7.所有交易中，最高交易额是多少
    @Test
    public void test7() {
        Integer max = transactionList.stream()
                .max(Comparator.comparingInt(Transaction::getValue))
                .get().getValue();
        System.out.println(max);
    }

    //8.找到交易额最小的交易
    @Test
    public void test8() {
        Optional<Transaction> min = transactionList.stream()
                .min(Comparator.comparingInt(Transaction::getValue));
        System.out.println(min.get());
    }

}
